package cn.script.game;


import cn.script.common.CoordConst;
import cn.script.domain.Point;
import cn.script.domain.RangePoint;
import cn.script.exception.StopException;
import cn.script.game.opt.Attack;
import cn.script.game.opt.DupOpt;
import cn.script.game.opt.MoveLocation;
import cn.script.opt.WinOpt;
import cn.script.util.Delay;
import cn.script.util.LogOut;

/**
 * 游戏操作
 *
 * @author 周永波
 */
public class GameOpt {

    private WinOpt wio;

    private GameInfo gi;


    public GameOpt(long hwnd) {
        this.wio = new WinOpt(hwnd);
        this.gi = new GameInfo(this.wio);
    }

    public GameInfo gi() {
        return this.gi;
    }

    public WinOpt wio() {
        return this.wio;
    }

    /**
     * 移动
     *
     * @return
     */
    public MoveLocation moveTo(String cityName) {
        return new MoveLocation(this.gi, cityName);
    }

    /**
     * 攻击怪物
     *
     * @return
     */
    public Attack attack(String attName) {
        return new Attack(this, attName);
    }

    /**
     * 副本打怪操作
     *
     * @param cityName
     * @param attName
     * @return
     */
    public DupOpt dupOpt(String cityName, String attName) {
        return new DupOpt(this, cityName, attName);
    }

    /**
     * 组队
     *
     * @param name
     * @return
     */
    public int team(String name, String city, GameOpt gop) {
        if (this.wio.findStr("%", "f8e000", RangePoint.rp(86, 168, 122, 179)) != null) {
            LogOut.out("队伍已存在");
            return 1;
        }

        int rx = 0;
        String[] sps = name.split("\\|");
        if (sps.length > 1) {
            rx = sps[0].length();
            name = sps[1];
        }
        Point offset = CoordConst.offset_name(name, rx);
        gop.toCity(city);
        gop.wio.leftClick(Point.point(204, 315), 1000);

        this.toCity(city);
        this.wio.leftClickStr(name, "f8fc00-000000|08fc80-000000", offset, CoordConst.WIN, 1000);
        Delay.delay(500);
        this.wio.rightClick(Point.point(441, 84), 500);
        this.wio.rightClick(Point.point(498, 139), 500);
        int num = 0;
        while (true) {
            Delay.delay(500);
            if (num > 5) {
                throw new StopException();
            }
            if (gop.bounced_text("接受") == 1) {
                return 1;
            }
            num++;
        }
    }

    public void reset() {
        int num = 0;
        while (true) {
            if (num>10){
                return;
            }
            if (gi.isReset()) {
                wio.keypress("esc", 200);
                return;
            }
            wio.keypress("esc", 200);
            num++;
        }
    }

    /**
     * 回城
     */
    public void toBack() {
        this.toCity("凌霄城");
    }

    /**
     * 回城市
     *
     * @param city
     */
    public void toCity(String city) {
        reset();
        if (!this.gi.isOpenBack()) {
            this.wio.keypress("f12", 500);
        }
        this.wio.leftClickStr(city, "f8e000", null, CoordConst.BACK);
        Delay.delay(1000);
        LogOut.out("回城->%s", city);
    }

    /**
     * npc 点击
     *
     * @param npcName
     */
    public int npc(String npcName) {
        Point point = CoordConst.offset_npc(npcName);
        int ret = this.wio.leftClickStr(npcName, "f8fc00-000000|08fc80-000000", point, CoordConst.WIN, 1000);
        LogOut.out("点击NPC->%s", npcName);
        return ret;
    }

    /**
     * 文本点击
     *
     * @param text
     */
    public int text_click(String text) {
        Point point = CoordConst.offset_text();
        int ret = this.wio.leftClickStr(text, "00fcf8-000000|00b8f8-000000|f0f4a0-000000|98fc00-000000|f8fcf8-000000|08fc80-000000|f8e000-000000", point, CoordConst.DIALOGUE, 500);
        LogOut.out("点击文本->%s", text);
        return ret;
    }

    /**
     * 文本点击
     *
     * @param text
     */
    public void text_click(String text, RangePoint rp) {
        Point point = CoordConst.offset_text();
        this.wio.leftClickStr(text, "98fc00-000000|f8fcf8-000000|08fc80-000000|f8e000-000000", point, rp, 500);
        LogOut.out("点击文本->%s", text);
    }

    /**
     * 弹框点击
     *
     * @param text
     */
    public int bounced_text(String text) {
        int ret = this.wio.leftClickStr(text, "f8e000", null, CoordConst.WIN, 500);
        if (ret == 0) {
            return 0;
        }
        LogOut.out("点击弹框->%s", text);
        return ret;
    }

    /**
     * 打开月光宝盒
     */
    public void openBox() {
        Point point = this.wio.findStr("月光宝盒", "f8e000", RangePoint.rp(134, 135, 655, 168));
        if (point == null) {
            this.wio.keypress("1", 500);
        }
        return;
    }

    /**
     * 点击天堂npc是否成功
     *
     * @param rp
     */
    public void clickNpcTt(RangePoint rp) {
        toBack();
        int num = 0;
        while (true){
            if (num>30){
                throw new StopException("神秘的天堂点击有问题;重置");
            }
            wio.moveToEx(rp,50);
            wio.rightClick(50);
            String attackName = gi.attackName();
            if (attackName.contains("神武兵团祭祀天朝")){
                wio.leftClick(200);
                return;
            }
            num++;
        }
    }
}
